﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Torimi.Wiki.Interfaces;

namespace Torimi.Wiki.Services
{
	/// <summary>
	/// Log4Net implementation of our ILogger interfact.
	/// </summary>
	public class Log4NetLogger : ILogger
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="Log4NetLogger"/> class.
		/// </summary>
		public Log4NetLogger()
		{
			Logger = log4net.LogManager.GetLogger(typeof(Log4NetLogger));
			log4net.Config.XmlConfigurator.Configure();
		}

		private ILog Logger { get; set; }

		/// <summary>
		/// Logs the warning.
		/// </summary>
		/// <param name="message">The message.</param>
		public void LogWarning(string message)
		{
			Logger.Warn(message);
		}

		/// <summary>
		/// Logs the error.
		/// </summary>
		/// <param name="message">The message.</param>
		public void LogError(string message)
		{
			Logger.Error(message);
		}

		/// <summary>
		/// Logs the error.
		/// </summary>
		/// <param name="exception">The exception.</param>
		public void LogError(Exception exception)
		{
			while (exception != null)
			{
				// Loop through to the innter most exceptions....
				while (exception.InnerException != null)
				{
					LogError(exception.InnerException);
				}
				// ...and then begin logging...
				Logger.Error(exception.ToString());
				break;
			}
		}

		/// <summary>
		/// Logs the information.
		/// </summary>
		/// <param name="message">The message.</param>
		public void LogInformation(string message)
		{
			Logger.Info(message);
		}

		/// <summary>
		/// Logs the fatal.
		/// </summary>
		/// <param name="message">The message.</param>
		public void LogFatal(string message)
		{
			Logger.Fatal(message);
		}
	}
}